package com.wutianyi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

@Controller
public class FileController {

	private ObjectMapper objectMapper = new ObjectMapper();

	private DatastoreService datastoreService = DatastoreServiceFactory
			.getDatastoreService();

	@RequestMapping("/list_file.do")
	public ResponseEntity<String> listFiles(
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "20") int pageSize)
			throws JsonGenerationException, JsonMappingException, IOException {

		Query query = new Query(BlobInfoFactory.KIND);
		query.addFilter(BlobInfoFactory.FILENAME, FilterOperator.NOT_EQUAL, "");
		int total = datastoreService.prepare(query).countEntities(
				FetchOptions.Builder.withDefaults());
		int offset = offset(total, page, pageSize);

		if (offset == 0) {
			offset = 1;
		}

		List<Entity> files = datastoreService.prepare(query).asList(
				FetchOptions.Builder.withLimit(pageSize).offset(offset));
		Map<String, Object> maps = new HashMap<String, Object>();
		List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
		for (Entity file : files) {
			Map<String, String> infos = new HashMap<String, String>();
			infos.put(BlobInfoFactory.FILENAME,
					(String) file.getProperty(BlobInfoFactory.FILENAME));
			infos.put(BlobInfoFactory.MD5_HASH,
					(String) file.getProperty(BlobInfoFactory.MD5_HASH));
			infos.put(BlobInfoFactory.CREATION,
					file.getProperty(BlobInfoFactory.CREATION).toString());
			lists.add(infos);
		}
		maps.put("files", lists);
		maps.put("counts", total);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		return new ResponseEntity<String>(
				objectMapper.writeValueAsString(maps), headers, HttpStatus.OK);
	}

	private static int offset(int total, int page, int pageSize) {
		if (page <= 0) {
			page = 1;
		}
		if (pageSize == 0) {
			pageSize = 20;
		}

		int tPage = total % pageSize == 0 ? total / page : ((total / page) + 1);
		if (page > tPage) {
			page = tPage;
		}
		int curOffet = (page - 1) * pageSize;
		return curOffet;
	}

	public static void main(String[] args) throws JsonGenerationException,
			JsonMappingException, IOException {
		ObjectMapper objectMapper = new ObjectMapper();
		List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
		Map<String, Object> maps = new HashMap<String, Object>();
		for (int i = 0; i < 10; i++) {
			Map<String, String> infos = new HashMap<String, String>();
			infos.put(BlobInfoFactory.FILENAME, "" + i);
			infos.put(BlobInfoFactory.MD5_HASH, "" + i);
			infos.put(BlobInfoFactory.CREATION, "" + i);
			lists.add(infos);
		}
		maps.put("files", lists);
		maps.put("count", 100);
		System.out.println(objectMapper.writeValueAsString(maps));
	}
}
